#!/bin/sh


# VAR

PRODUCT=lucc_fax
DEBUG=0
STEP=1
INTOOLS=`pwd`
INTOOLS=`basename $INTOOLS`
BACKUP_DIR="/var/preserve/lucc_sysbak"
DOT=0

# FUNC

function lucc_title()
{
	echo
	echo
	echo "Setup LuccFAX:"
	echo
	echo -e "\tYou must obtain the license of LuccFAX!"
	echo
}

function lucc_step()
{
	echo
	echo "$STEP. $1"
	echo 
	DOT=0
	STEP=`expr $STEP + 1`
	if [ "$DEBUG" = "0" ];
	then
		echo -e -n "\t"
	fi
}

function lucc_end()
{
	echo
	echo -e "e. $1"
	echo
	echo
}

function lucc_exec()
{
	if [ "$DEBUG" != "0" ];
	then
		echo -e "\t$*"
	else
		echo -n "."
		DOT=`expr $DOT + 1`
		if [ $DOT -ge 30 ];
		then
			echo -n -e "\n\t."
			DOT=1
		fi
	fi

	$* >/dev/null
	if [ "$?" != "0" ];
	then
		if [ "$DEBUG" = "0" ];
		then
			echo
			echo
		fi

		lucc_end "\texec failed, aborted!"
		exit 1
	fi
}

function lucc_ok()
{
	if [ "$DEBUG" = "0" ];
	then
		echo "ok"
	else
		echo -e "\tok"
	fi
}

function lucc_prompt()
{
	while :
	do
		read -p "	do you want to continue? [y|n]  " INPUT
		if [ "$INPUT" = "y" ];
		then
			break
		elif [ "$INPUT" = "n" ];
		then
			lucc_end "Thank you!"
			exit 0
		fi
		
	done
}

function lucc_sleep()
{
	count=$1
	i=1

	while [ "$i" != "$count" ];
	do
		echo -n "."
		i=`expr $i + 1`
	done
}

function lucc_service()
{
	op=$1
	name=$2

	if [ ! -e /etc/init.d/$name ];
	then
		return
	fi

	case "$op" in
		on)
		chkconfig $name on >/dev/null 2>/dev/null
		;;
		off)
		chkconfig --level 0123456 $name off >/dev/null 2>/dev/null
		;;
		start)
		/etc/init.d/$name start >/dev/null 2>/dev/null
		;;
		stop)
		/etc/init.d/$name stop >/dev/null 2>/dev/null
		;;
		restart)
		/etc/init.d/$name restart >/dev/null 2>/dev/null
		;;
		add)
		chkconfig --add $name >/dev/null 2>/dev/null
		;;
	esac
}

function lucc_check()
{
	pid=`pidof -x $1`
	if [ -z "$pid" ];
	then
		lucc_end "\t$1 not running, aborted!"
		exit
	else
		echo -n "."
	fi
}

# MAIN

lucc_title

if [ "$INTOOLS" = "tools" ];
then
	cd ..
elif [ "$INTOOLS" != "$PRODUCT" ];
then
	lucc_end "You are not in correct path."
	exit 0
fi

# BACKUP

lucc_prompt

if [ ! -e $BACKUP_DIR ];
then
	lucc_step "backup system"
		lucc_exec mkdir -p $BACKUP_DIR/etc/
		lucc_exec mkdir -p $BACKUP_DIR/var/lib/
		lucc_exec mkdir -p $BACKUP_DIR/var/www/
		lucc_exec mkdir -p $BACKUP_DIR/etc/http/conf.d/
		lucc_exec mkdir -p $BACKUP_DIR/etc/http/conf
		lucc_exec mkdir -p $BACKUP_DIR/etc/init.d/
		lucc_exec mkdir -p $BACKUP_DIR/root/
		lucc_exec mkdir -p $BACKUP_DIR/etc/cron.d/
		lucc_exec mkdir -p $BACKUP_DIR/usr/sbin/
		lucc_exec mkdir -p $BACKUP_DIR/bin/

                lucc_exec cp -r /etc/asterisk $BACKUP_DIR/etc/
                lucc_exec cp -r /var/lib/asterisk $BACKUP_DIR/var/lib/
		lucc_exec cp -dpr /var/www/html $BACKUP_DIR/var/www/
		lucc_exec cp /etc/sysctl.conf $BACKUP_DIR/etc/
		lucc_exec cp /etc/sudoers $BACKUP_DIR/etc/
		lucc_exec cp /etc/hosts $BACKUP_DIR/etc/
		lucc_exec cp /etc/crontab $BACKUP_DIR/etc/
		lucc_exec cp /etc/syslog.conf $BACKUP_DIR/etc/
		lucc_exec cp /etc/php.ini $BACKUP_DIR/etc/
		lucc_exec cp /etc/my.cnf $BACKUP_DIR/etc/
		lucc_exec cp /etc/ntp.conf $BACKUP_DIR/etc/
		lucc_exec cp /etc/httpd/conf/httpd.conf $BACKUP_DIR/etc/http/conf/
		lucc_exec cp /root/.bashrc $BACKUP_DIR/root/
		lucc_exec cp /bin/vi $BACKUP_DIR/bin/

		lucc_ok
fi

# CLEAN

lucc_step "clean system"
	lucc_service stop httpd
	lucc_service stop vsftpd
	lucc_service stop smb
	lucc_service stop lucc

	lucc_service off openfire
	lucc_service off wanrouter
	lucc_service off cyrus-imapd
	lucc_service off postfix

	lucc_ok

# CONFIG

lucc_step "config system"
	lucc_service on httpd
	lucc_service on vsftpd
	lucc_service on smb
	lucc_service on mysqld
	lucc_service on asterisk

	diff system/my.cnf /etc/my.cnf >/dev/null 2>/dev/null
	if [ "$?" != "0" ];
	then
		lucc_exec cp system/my.cnf /etc/
		lucc_service stop mysqld
	fi
	lucc_service start mysqld

	lucc_exec mkdir -p /etc/samba
	lucc_exec mkdir -p /home/samba
	lucc_exec mkdir -p /var/www/html/web/
	lucc_exec mkdir -p /lucc
	lucc_exec mkdir -p /lucc/fax/source
	lucc_exec mkdir -p /lucc/fax/from
	lucc_exec mkdir -p /lucc/fax/in
	lucc_exec mkdir -p /lucc/fax/barcode
	lucc_exec mkdir -p /lucc/fax/stamp
	lucc_exec mkdir -p /usr/local/lucc
	lucc_exec mkdir -p /var/log/lucc
	lucc_exec mkdir -p /var/spool/$PRODUCT

	lucc_exec cp -r web/* /var/www/html/web/

	lucc_exec cp system/smb.conf /etc/samba/
	lucc_exec cp system/vimrc /root/.vimrc
	lucc_exec cp system/sysctl.conf /etc/
	lucc_exec cp system/sudoers /etc/
	lucc_exec cp system/sampler.php /usr/local/lucc
	lucc_exec cp system/lucc.conf /etc/httpd/conf.d/
	lucc_exec cp system/lucc_safe.conf /etc/httpd/conf.d/
	lucc_exec cp system/httpd.conf /etc/httpd/conf/
	lucc_exec cp system/php.ini /etc/
	lucc_exec cp system/bashrc /root/.bashrc
	lucc_exec cp system/lucc-clean /etc/cron.daily/
	lucc_exec chmod +x /etc/cron.daily/lucc-clean
	lucc_exec cp sounds/system/*.wav /var/lib/asterisk/sounds/system/
	lucc_exec cp sounds/system/*.gsm /var/lib/asterisk/sounds/system/
	lucc_exec cp db/db.sh /lucc/
	lucc_exec cp db/*.sql /lucc/
	lucc_exec cp web/home.php /var/www/html/index.php
	lucc_exec cp web/favicon.ico /var/www/html/favicon.ico
	lucc_exec cp lucc/* /lucc
	lucc_exec rm -f /lucc/update.sh

	lucc_exec sysctl -p /etc/sysctl.conf
	if [ -e /etc/init.d/ktune ];
	then
		lucc_service on ktune
		/etc/init.d/ktune start >/dev/null 2>/dev/null
	fi

	lucc_exec killall -s HUP syslogd

	lucc_exec find /var/www/html/web/ -name \*.sh -exec rm {} \;
	lucc_exec find /var/www/html/web/ -name \*.php -exec chmod 644 {} \;
	lucc_exec find /var/www/html/web/ -name \*.tpl -exec chmod 644 {} \;
	lucc_exec find /var/www/html/web/ -type d -exec chmod 755 {} \;

	if [ ! -e /var/spool/$PRODUCT/installed ];
	then
		lucc_exec cd db
		lucc_exec ./db_first.sh
		lucc_exec cd -
		lucc_exec touch /var/spool/$PRODUCT/installed
	fi
	lucc_exec cd db
	lucc_exec ./db.sh
	lucc_exec cd -

	lucc_ok


# STARTUP

lucc_step "startup"
	lucc_exec ldconfig
	lucc_service start httpd
	lucc_service start vsftpd
	lucc_service start smb
	lucc_service add lucc
	lucc_service on lucc
	lucc_service start lucc
	sleep 3

	if [ "`pidof luccd`" != "" ];
	then
		lucc_exec php /lucc/cmd.php load_all
	fi

	lucc_ok

# CHECK

lucc_step "check"

	lucc_ok

# END

lucc_end "finished, thank you!"

